package view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import bean.Cliente;
import rmi.ClienteRMI;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class EliminarCliente extends javax.swing.JFrame {
	private JButton btnCerrar;
	private JButton btnEliminar;
	private AbstractAction eliminarAction;
	private AbstractAction cerrarAction;
	private JLabel lblCliente;
	private JComboBox cmbClientes;
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				EliminarCliente inst = new EliminarCliente();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public EliminarCliente() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Eliminar Cliente");
			this.setPreferredSize(new java.awt.Dimension(400, 150));
			{
				lblCliente = new JLabel();
				getContentPane().add(lblCliente);
				lblCliente.setText("SELECCIONAR CLIENTE: ");
				lblCliente.setBounds(6, 22, 138, 14);
			}
			{
				Vector vc = new Vector();
				if (ClienteRMI.getInstance().getIRemota().getClientes() != null){
					vc = getClientesViewVector(ClienteRMI.getInstance().getIRemota().getClientes());
					
				}else{
					JOptionPane.showMessageDialog(null, "No se encuentran clientes a eliminar.", "No existen clientes", JOptionPane.WARNING_MESSAGE);
				}
				cmbClientes = new JComboBox(vc);
				getContentPane().add(cmbClientes);
				cmbClientes.setBounds(156, 19, 225, 21);				
			}
			{
				btnEliminar = new JButton();
				getContentPane().add(btnEliminar);
				btnEliminar.setText("Eliminar");
				btnEliminar.setBounds(86, 66, 92, 33);
				btnEliminar.setFont(new java.awt.Font("Tahoma",1,11));
				btnEliminar.setAction(getEliminarAction());
			}
			{
				btnCerrar = new JButton();
				getContentPane().add(btnCerrar);
				btnCerrar.setText("Cerrar");
				btnCerrar.setBounds(214, 67, 92, 32);
				btnCerrar.setFont(new java.awt.Font("Tahoma",1,11));
				btnCerrar.setAction(getCerrarAction());
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	private AbstractAction getEliminarAction() {
		if(eliminarAction == null) {
			eliminarAction = new AbstractAction("Eliminar", null) {
				public void actionPerformed(ActionEvent evt) {
					try{
						String cuit = cmbClientes.getSelectedItem().toString().substring(0,11);
						Cliente c = new Cliente(cuit);
						ClienteRMI.getInstance().getIRemota().bajaCliente(c);
						cmbClientes.removeItemAt(cmbClientes.getSelectedIndex());
						JOptionPane.showMessageDialog(null, "Cliente eliminado con exito.", "Resultado", JOptionPane.INFORMATION_MESSAGE);
					}catch  (Exception e){
						JOptionPane.showMessageDialog(null, "No se encuentran clientes a eliminar.", "Error en la carga de datos", JOptionPane.WARNING_MESSAGE);
						dispose();
					}
				}
			};
		}
		return eliminarAction;
	}
	
	
	private AbstractAction getCerrarAction() {
		if(cerrarAction == null) {
			cerrarAction = new AbstractAction("Cerrar", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return cerrarAction;
	}
	
	
	public Vector getClientesViewVector(Vector vc){ 
		Vector mv = new Vector();
		for (int i= 0; i < vc.size(); i++){
			Cliente c = (Cliente) vc.elementAt(i);
			String aux = String.valueOf(c.getCuit()) + "   " + c.getRazonSocial(); 
			mv.add(aux);
		}
		return mv;
	}

	
}
